<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">

    <link rel="stylesheet" href="../bootstrap-3.3.4/dist/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="../Flat-UI-master/dist/css/flat-ui.min.css"/>
    <script src="../Flat-UI-master/dist/js/vendor/jquery.min.js"></script>
    <script src="../bootstrap-3.3.4/dist/js/bootstrap.min.js"></script>
    <script src="../Flat-UI-master/dist/js/flat-ui.min.js"></script>
    <title></title>
    <style>
        .row{
            margin-left: 20px;
            margin-right: 20px;;
        }
    </style>
    <script src="../js/jquery-3.2.1.min.js"></script>
</head>
<body>
<!-- Static navbar -->
<div class="navbar navbar-default navbar-static-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">这什么东西</span>
            </button>
            <a class="navbar-brand" href="Index.html">图书商城</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="Index.html">首页</a></li>
                <li><a href="Order.html">我的订单</a></li>
                <li><a href="UserInfo.html">个人中心</a></li>
                <li hidden><a href="FriendLink.html">友情链接</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right hidden-sm">
                <li><a href="Login.html">登录</a></li>
                <li><a href="Register.html">注册</a></li>
                <li>
                    <a href="Cart.html"><span class="glyphicon glyphicon-shopping-cart">购物车</span></a></li>
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</div>
<!--content-->
<div class="container">
    <div class="row thumbnail center">
        <div class="col-sm-12">
            <h1 class="text-center" style="margin-bottom: 30px">用户登录</h1>
        </div>
        <div class="col-sm-6">
            <div class="form-horizontal caption">
                <div class="form-group">
                    <label for="username" class="col-sm-3 control-label">用户名</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="username" placeholder="用户名">
                        <span id="userspan"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="password" class="col-sm-3 control-label">密码</label>
                    <div class="col-sm-8">
                        <input type="password" class="form-control" id="password" placeholder="密码">
                        <span id="passspan"></span>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-9">
                        <div class="checkbox">
                            <label hidden>
                                <input type="checkbox" id="remember">记住我
                            </label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-5">
                        <button type="button" class="btn btn-success btn-block" id="login">登录</button>
                        <span id="span"></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="caption">
                <!--<h3>免责声明</h3>-->
                <!--<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>-->

            </div>

        </div>

    </div>
</div>


<!--footer-->
<div class="navbar navbar-default navbar-static-bottom">
    版权声明区
</div>
<script>
    window.onload=function () {
        $.ajax({
            url:"/session",
            type:"get",
            success:function (re) {
                if (re.code == 1000) {
                    if (re.data.state == 1) {
                        location.href="/Home/Index.html?username="+re.data.username;
                    }else if (re.data.state == 2) {
                        location.href="/adminjsps/admin/main.jsp?username="+re.data.username;
                    }
                }else {
                    console.log("之前没登录")
                }

            }
        })
    }
    var userbool = false;
    var passbool = false;
    $("#username").click(function () {
        $("#userspan").empty();
    });
    $("#username").blur(function () {
        var username = $("#username").val();
        var user = /[A-Za-z0-9_\-\u4e00-\u9fa5]{4,16}/;
        console.log($("#username").val());
        console.log(userbool);
        console.log(user.test(username));
        if (user.test(username)) {
            userbool = true;
            $("#userspan").html("符合要求");
        }else {
            userbool = false;
            $("#userspan").html("用户名必须在4-16位范围内");
        }
    });
    $("#password").click(function () {
        $("#passspan").empty();
    });
    $("#password").blur(function () {
        var password = $("#password").val();
        var pass = /[A-Za-z0-9_\-\u4e00-\u9fa5]{4,16}/;
        if (pass.test(password)) {
            passbool = true;
            $("#passspan").html("符合要求");
        }else {
            userbool = false;
            $("#passspan").html("密码必须在4-16位范围内");
        }
    });
    $("#login").click(function () {
        var username = $("#username").val();
        var password = $("#password").val();
        var check = $("#remember").prop('checked');
        if (userbool && passbool) {
            $.ajax({
                url:"/login",
                type:"get",
                data:{
                    username:username,
                    password:password,
                    check:check
                },
                success:function (re) {
                    if (re.code == 1000) {
                        if (re.data.state == 1) {
                            location.href="/Home/Index.html?username="+re.data.username;
                        }else if (re.data.state == 2) {
                            location.href="/adminjsps/admin/main.jsp?username="+re.data.username;
                        }

                    }else {
                        // alert(re.msg);
                        console.log(re.msg)
                        $("#span").html(re.msg)
                    }
                }
            })
        }
    })
</script>
</body>
</html>